/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing.jee.web.view;

import com.vaadin.ui.TextField;
import testing.jee.util.DurationUtil;

/**
 *
 * @author kreisera
 */
public class DurationField extends TextField {

    @Override
    public void setValue(Object newValue) throws ReadOnlyException, ConversionException {
        int duration = 0;
        if (newValue != null) {
            try {
                duration = Integer.parseInt((String) newValue);
            } catch (Exception ex) {
            }
        }
        String serialized = DurationUtil.serializeDuration(duration);
        super.setValue(serialized, false);
    }

    @Override
    public Object getValue() {
        Object value = super.getValue();
        if (value instanceof Integer) {
            return value;
        }
        return DurationUtil.convertToHours((String) value);
    }
}